﻿using RXY_Mall.Application.Finance;
using RXY_Mall.Application.Manager;
using RXY_Mall.Application.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Shop.Controllers
{
    public class HomeController : BaseController
    {
        protected IManagerService _managerService;
        private IFinanceService _financeService;
        private IMessageService _messageService;
        public HomeController()
        {
            _managerService = new ManagerService();
            _financeService = new FinanceService();
            _messageService = new MessageService();
        }

        public ActionResult Index()
        {
            ViewBag.Balances = 0.00;
            ViewBag.Balance = 0.00;
            var balance = _financeService.GetShopBalances(ShopId);
            if (balance != null)
            {
                ViewBag.Balances = balance.balances + balance.balance_freeze;
                ViewBag.Balance = (balance.balances - balance.balance_due) > 0 ? balance.balances - balance.balance_due : 0;
            }
            ViewBag.Settlementing = _financeService.GetSettlementing(ShopId);
            ViewBag.ShippingNumber = _financeService.GetOrdersNumber(ShopId,4);
            ViewBag.ReceivingNumber = _financeService.GetOrdersNumber(ShopId,5);
            ViewBag.GoodsNumber1 = _financeService.GetGoodsNumber(ShopId,1);
            ViewBag.GoodsNumber0 = _financeService.GetGoodsNumber(ShopId, 0);
            var model = _financeService.GetOrderTotal(ShopId);
            return View(model);
        }
        public JsonResult GetUserName()
        {
            return Json(UserName,JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetMessage()
        {
            int totalCount = 0;
            var list = _messageService.GetTopMessage(5, ShopId, out totalCount);
            return Json(new { list ,totalCount}, JsonRequestBehavior.AllowGet);
        }

        //设为已读
        [HttpPost]
        public async Task<ActionResult> ReadNotify()
        {
            var result =await _messageService.ReadMessage(ShopId);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
    }
}